﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using FinalTestMachine.Motion.Aixs;
using FinalTestMachine.Motion.Process;

namespace FinalTestMachine.View.MainUnits
{
    /// <summary>
    /// Wnd_DailyCheck.xaml 的交互逻辑
    /// </summary>
    public partial class Wnd_DailyCheck : Window
    {
        private static readonly object ob_lock = new object();
        private static Wnd_DailyCheck Cur = null;

        private Wnd_DailyCheck()
        {
            InitializeComponent();
        }

        private bool[] dailyCheckEverySiteProductState = new bool[]
        {
            false, false, false, false, false, false, false, false
        };

        public static Wnd_DailyCheck CreateInstrance()
        {
            if (Cur == null)
            {
                lock (ob_lock)
                {
                    if (Cur == null)
                    {
                        Cur = new Wnd_DailyCheck();
                    }
                }
            }

            return Cur;
        }

        public static void SingleShow()
        {
            Window window = CreateInstrance();
            window.Owner = App.Current.MainWindow;
            window.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            window.WindowState = WindowState.Normal;
            window.Show();
        }

        private bool IsExit = false;

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Task.Run(() => ShowUI());
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            Cur = null;
            IsExit = true;
        }

        private void ShowUI()
        {
            bool IsCanScan = true;
            while (!IsExit)
            {
                if (IsCanScan)
                {
                    IsCanScan = false;
                    this.Dispatcher.BeginInvoke(new Action(() =>
                    {
                        if (!RotatedMovNextHelper.IsOkPos || Aix_Turn.Cur.IsBusy || Aix_Turn.Cur.GetAixRunState()
                        ||/* FirstTestStation.Cur.IsTesting ||*/ SecondTestStation.Cur.IsTesting || ThirdTestStation.Cur.IsTesting || FourTestStation.Cur.IsTesting)
                        {
                          //  btn_Site1StartTest.IsEnabled = false;
                            btn_Site2StartTest.IsEnabled = false;
                            btn_Site3StartTest.IsEnabled = false;
                            btn_Site4StartTest.IsEnabled = false;
                        }
                        else
                        {
                         //   btn_Site1StartTest.IsEnabled = true;
                            btn_Site2StartTest.IsEnabled = true;
                            btn_Site3StartTest.IsEnabled = true;
                            btn_Site4StartTest.IsEnabled = true;
                        }

                        IsCanScan = true;
                    }));
                }
                bool checkState = true;

                foreach (var item in dailyCheckEverySiteProductState)
                {
                    if (item == true)
                        continue;
                    if (item == false)
                        checkState = false;
                }
                if (checkState)
                {
                    MessageBox.Show("点检成功，请点击确认按钮回到主界面后点击启动按钮进入自动生产模式");
                    Funs.FunParam.DailyCheckSuccess = true;
                    this.Dispatcher.Invoke(() => this.Close());
                }

                Thread.Sleep(100);
            }
        }

        private async void btn_Site1StartTest_Click(object sender, RoutedEventArgs e)
        {

        }

        private async void btn_Site2StartTest_Click(object sender, RoutedEventArgs e)
        {
            if (Motion.StaticInitial.Motion.CurOrder.IsResOK)
            {
                if (comboxSite2ProductState.SelectedItem == null)
                {
                    MessageBox.Show("请先选择工位2产品状态");
                    return;
                }

                try
                {
                    Motion.Aixs.Aix_Turn.Cur.Mov_xPos(1);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }
                SecondTestStation.Cur.LoadProduct(new Motion.Product.ProductInfo());
                await Task.Run(() => { SecondTestStation.Cur.Run(); });
                string str = comboxSite2ProductState.SelectedValue.ToString();
                System.Windows.Controls.ComboBoxItem item = (System.Windows.Controls.ComboBoxItem)comboxSite2ProductState.SelectedItem;
                switch (item.Content)
                {
                    case "OK品":
                        if (SecondTestStation.Cur.IsTestOK)
                        {
                            textBlockSite2OKProductDailyCheckResult.Background = Brushes.Green;
                            dailyCheckEverySiteProductState[2] = true;
                        }
                        else
                        {
                            textBlockSite2OKProductDailyCheckResult.Background = Brushes.Red;
                        }
                        break;

                    case "NG品":
                        if (SecondTestStation.Cur.IsTestOK)
                        {
                            textBlockSite2NGProductDailyCheckResult.Background = Brushes.Red;
                        }
                        else
                        {
                            textBlockSite2NGProductDailyCheckResult.Background = Brushes.Green;
                            dailyCheckEverySiteProductState[3] = true;
                        }
                        break;
                }
                try
                {
                    Motion.Aixs.Aix_Turn.Cur.Mov_0();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }
                SecondTestStation.Cur.LoadProduct(null);
            }
            else
            {
                MessageBox.Show("请先复位设备再进行点检操作");
            }
        }

        private async void btn_Site3StartTest_Click(object sender, RoutedEventArgs e)
        {
            if (Motion.StaticInitial.Motion.CurOrder.IsResOK)
            {
                if (comboxSite3ProductState.SelectedItem == null)
                {
                    MessageBox.Show("请先选择工位2产品状态");
                    return;
                }

                try
                {
                    Motion.Aixs.Aix_Turn.Cur.Mov_xPos(2);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }

                ThirdTestStation.Cur.LoadProduct(new Motion.Product.ProductInfo());

                await Task.Run(() => { ThirdTestStation.Cur.Run(); });
                System.Windows.Controls.ComboBoxItem item = (System.Windows.Controls.ComboBoxItem)comboxSite3ProductState.SelectedItem;
                switch (item.Content)
                {
                    case "OK品":
                        if (ThirdTestStation.Cur.IsTestOK)
                        {
                            textBlockSite3OKProductDailyCheckResult.Background = Brushes.Green;
                            dailyCheckEverySiteProductState[4] = true;
                        }
                        else
                        {
                            textBlockSite3OKProductDailyCheckResult.Background = Brushes.Red;
                        }
                        break;

                    case "NG品":
                        if (ThirdTestStation.Cur.IsTestOK)
                        {
                            textBlockSite3NGProductDailyCheckResult.Background = Brushes.Red;
                        }
                        else
                        {
                            textBlockSite3NGProductDailyCheckResult.Background = Brushes.Green;
                            dailyCheckEverySiteProductState[5] = true;
                        }
                        break;
                }

                try
                {
                    Motion.Aixs.Aix_Turn.Cur.Mov_0();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }

                ThirdTestStation.Cur.LoadProduct(null);
            }
            else
            {
                MessageBox.Show("请先复位设备再进行点检操作");
            }
        }

        private async void btn_Site4StartTest_Click(object sender, RoutedEventArgs e)
        {
            if (Motion.StaticInitial.Motion.CurOrder.IsResOK)
            {
                if (comboxSite4ProductState.SelectedItem == null)
                {
                    MessageBox.Show("请先选择工位2产品状态");
                    return;
                }

                try
                {
                    Motion.Aixs.Aix_Turn.Cur.Mov_xPos(3);
                }
                catch (Exception ex) { MessageBox.Show(ex.Message); return; }
                FourTestStation.Cur.LoadProduct(new Motion.Product.ProductInfo());

                await Task.Run(() => { FourTestStation.Cur.Run(); });
                System.Windows.Controls.ComboBoxItem item = (System.Windows.Controls.ComboBoxItem)comboxSite4ProductState.SelectedItem;
                switch (item.Content)
                {
                    case "OK品":
                        if (FourTestStation.Cur.IsTestOK)
                        {
                            textBlockSite4OKProductDailyCheckResult.Background = Brushes.Green;
                            dailyCheckEverySiteProductState[6] = true;
                        }
                        else
                        {
                            textBlockSite4OKProductDailyCheckResult.Background = Brushes.Red;
                        }
                        break;

                    case "NG品":
                        if (FourTestStation.Cur.IsTestOK)
                        {
                            textBlockSite4NGProductDailyCheckResult.Background = Brushes.Red;
                        }
                        else
                        {
                            textBlockSite4NGProductDailyCheckResult.Background = Brushes.Green;
                            dailyCheckEverySiteProductState[7] = true;
                        }
                        break;
                }
                try
                {
                    Motion.Aixs.Aix_Turn.Cur.Mov_0();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }
                FourTestStation.Cur.LoadProduct(null);
            }
            else
            {
                MessageBox.Show("请先复位设备再进行点检操作");
            }
        }
    }
}